/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       bitmap_group_cache.cpp

	$Header: /game/bitmap_group_cache.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "bitmap_group_cache.h"

#include "abstract_resource_cache.h"
#include "bitmap_group.h"
#include "bitmap_group_cache_data.h"
#include "scale_bitmap.h"

namespace
{
	// ----------------------------------------------------------
	// cache data for a scaled bitmap
	// ----------------------------------------------------------
	class t_scaled_bitmap_group_cache_data : public t_bitmap_group_cache_data
	{
	public:
		t_scaled_bitmap_group_cache_data( std::string const& name, double scale );
	protected:
		virtual t_bitmap_group* do_read( std::streambuf & stream, t_progress_handler* handler );

		double m_scale;
	};

	inline t_scaled_bitmap_group_cache_data::t_scaled_bitmap_group_cache_data(
										     std::string const& name, double scale )
			: t_bitmap_group_cache_data( name )
	{
		m_scale = scale;
	}

}; // unnamed namespace

// ----------------------------------------------------------
// cache data for an unscaled bitmap
// ----------------------------------------------------------
char const * t_bitmap_group_cache_data::get_prefix() const
{
	return t_resource_traits< t_bitmap_group_24 >::prefix;
}

// ----------------------------------------------------------
// cache data for an unscaled bitmap
// ----------------------------------------------------------
t_bitmap_group* t_bitmap_group_cache_data::do_read( std::streambuf & stream,
												    t_progress_handler* handler )
{
	t_bitmap_group*   result = 0;
	t_bitmap_group_24 source;

	if ( source.read( stream, handler ))
		result = new t_bitmap_group( source );
	return result;
}

// ----------------------------------------------------------
// cache data for an scaled bitmap
// ----------------------------------------------------------
t_bitmap_group* t_scaled_bitmap_group_cache_data::do_read( std::streambuf & stream, 
														   t_progress_handler* handler )
{
	t_bitmap_group*   result = 0;
	t_bitmap_group_24 source;

	if ( !source.read( stream, handler ))
		return 0;

	result = new t_bitmap_group;
	scale_group( source, *result, m_scale );
	return result;
}

// ----------------------------------------------------------
// cache a bitmap
// ----------------------------------------------------------
t_bitmap_group_cache::t_bitmap_group_cache( std::string const & name, double scale )
		            : t_abstract_cache<t_bitmap_group>( 0 )
{
	if (scale == 1.0)
		set( new t_bitmap_group_cache_data( name ) );
	else
		set( new t_scaled_bitmap_group_cache_data( name, scale ) );
}

